﻿using AutoMapper;
using DTcms.Core.Model.Models;
using DTcms.Core.Model.ViewModels;
using DTcms.Core.Common.Helper;
using DTcms.Core.Common.Extensions;

namespace DTcms.Core.Model.Profiles
{
    public class ArticleProfile : Profile
    {
        /// <summary>
        /// 内容实体映射
        /// </summary>
        public ArticleProfile()
        {

            #region 文章主表
            //源数据映射到DTO
            CreateMap<Articles, ArticlesDto>()
                .ForMember(
                    dest => dest.CategoryTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.Category!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.LabelTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.LabelRelations.Select(x => x.Label!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.Fields,
                    opt =>
                    {
                        opt.MapFrom(src => GetFields(src.ArticleFields));
                    }
                ).ForMember(
                    dest => dest.IsComment,
                    opt =>
                    {
                        opt.MapFrom(src => (src.SiteChannel != null && src.SiteChannel.IsComment == 1) ? src.IsComment : 0);
                    }
                );
            //源数据映射到DTO
            CreateMap<Articles, ArticlesClientDto>()
                .ForMember(
                    dest => dest.CategoryTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.Category!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.LabelTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.LabelRelations.Select(x => x.Label!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.Fields,
                    opt =>
                    {
                        opt.MapFrom(src => GetFields(src.ArticleFields));
                    }
                ).ForMember(
                    dest => dest.IsComment,
                    opt =>
                    {
                        opt.MapFrom(src => (src.SiteChannel != null && src.SiteChannel.IsComment == 1) ? src.IsComment : 0);
                    }
                );
            CreateMap<Articles, ArticlesEditDto>();
            //DTO映射到源数据
            CreateMap<ArticlesAddDto, Articles>();
            CreateMap<ArticlesEditDto, Articles>();
            //类别关系
            CreateMap<ArticleCategoryRelation, ArticleCategoryRelationDto>();
            CreateMap<ArticleCategoryRelationDto, ArticleCategoryRelation>();
            //标签关系
            CreateMap<ArticleLabelRelation, ArticleLabelRelationDto>();
            CreateMap<ArticleLabelRelationDto, ArticleLabelRelation>();
            #endregion

            #region 文章相册
            //源数据映射到DTO
            CreateMap<ArticleAlbum, ArticleAlbumDto>();
            //DTO映射到源数据
            CreateMap<ArticleAlbumDto, ArticleAlbum>();
            #endregion

            #region 文章附件
            //源数据映射到DTO
            CreateMap<ArticleAttach, ArticleAttachDto>()
                .ForMember(
                    dest => dest.Size,
                    opt =>
                    {
                        opt.MapFrom(src => FileHelper.ByteConvertStorage(src.FileSize));
                    }
                );
            CreateMap<ArticleAttach, ArticleAttachClientDto>()
                .ForMember(
                    dest => dest.Size,
                    opt =>
                    {
                        opt.MapFrom(src => FileHelper.ByteConvertStorage(src.FileSize));
                    }
                );
            //DTO映射到源数据
            CreateMap<ArticleAttachDto, ArticleAttach>();
            #endregion

            #region 文章分类
            //源数据映射到DTO
            CreateMap<ArticleCategory, ArticleCategoryDto>();
            CreateMap<ArticleCategory, ArticleCategoryEditDto>();
            //DTO映射到源数据
            CreateMap<ArticleCategoryEditDto, ArticleCategory>();
            #endregion

            #region 文章投稿
            //源数据映射到DTO
            CreateMap<ArticleContribute, ArticleContributeDto>();
            CreateMap<ArticleContribute, ArticleContributeViewDto>()
                .ForMember(dest => dest.Fields, opt => opt.MapFrom(src => JsonHelper.ToObject<List<ArticleContributeFieldValueDto>>(src.FieldMeta)))
                .ForMember(dest => dest.Albums, opt => opt.MapFrom(src => JsonHelper.ToObject<List<ArticleAlbumDto>>(src.AlbumMeta)))
                .ForMember(dest => dest.Attachs, opt => opt.MapFrom(src => JsonHelper.ToObject<List<ArticleAttachDto>>(src.AttachMeta)));
            //DTO映射到源数据
            CreateMap<ArticleContributeAddDto, ArticleContribute>()
                .ForMember(desc => desc.FieldMeta, opt => opt.MapFrom(src => src.Fields != null ? src.Fields.ToJson() : null))
                .ForMember(desc => desc.AlbumMeta, opt => opt.MapFrom(src => src.Albums != null ? src.Albums!.ToJson() : null))
                .ForMember(desc => desc.AttachMeta, opt => opt.MapFrom(src => src.Attachs != null ? src.Attachs!.ToJson() : null));
            CreateMap<ArticleContributeEditDto, ArticleContribute>()
                .ForMember(desc => desc.FieldMeta, opt => opt.MapFrom(src => src.Fields != null ? src.Fields!.ToJson() : null))
                .ForMember(desc => desc.AlbumMeta, opt => opt.MapFrom(src => src.Albums != null ? src.Albums!.ToJson() : null))
                .ForMember(desc => desc.AttachMeta, opt => opt.MapFrom(src => src.Attachs != null ? src.Attachs!.ToJson() : null));
            #endregion

            #region 文章评论
            //源数据映射到DTO
            CreateMap<ArticleComment, ArticleCommentDto>()
                .ForMember(
                    dest => dest.UserAvatar,
                    opt =>
                    {
                        opt.MapFrom(src => src.User != null && src.User.Member != null ? src.User.Member.Avatar : null);
                    }
                )
                .ForMember(
                    dest => dest.CopyContent,
                    opt =>
                    {
                        opt.MapFrom(src => (src.IsDelete == 1 ? src.Content : string.Empty));
                    }
                )
                .ForMember(
                    dest => dest.Content,
                    opt =>
                    {
                        opt.MapFrom(src => (src.IsDelete == 1 ? "原内容已删除" : src.Content));
                    }
                )
                .ForMember(
                    dest => dest.DateDescription,
                    opt =>
                    {
                        opt.MapFrom(src => src.AddTime.ToTimeDifferNow());
                    }
                );
            CreateMap<ArticleComment, ArticleCommentAddDto>();
            //DTO映射到源数据
            CreateMap<ArticleCommentDto, ArticleComment>();
            CreateMap<ArticleCommentAddDto, ArticleComment>();
            #endregion

            #region 扩展字段
            //源数据映射到DTO
            CreateMap<ArticleFieldValue, ArticleFieldValueDto>();
            CreateMap<ArticleFieldValue, ArticleFieldValueEditDto>();
            //DTO映射到源数据
            CreateMap<ArticleFieldValueEditDto, ArticleFieldValue>();
            CreateMap<ArticleFieldValueEditDto, ArticleFieldValue>();
            #endregion

            #region 文章标签
            //源数据映射到DTO
            CreateMap<ArticleLabel, ArticleLabelDto>();
            CreateMap<ArticleLabel, ArticleLabelEditDto>();
            //DTO映射到源数据
            CreateMap<ArticleLabelAddDto, ArticleLabel>();
            CreateMap<ArticleLabelEditDto, ArticleLabel>();
            #endregion

            #region 文章点赞
            //源数据映射到DTO
            CreateMap<ArticleLike, ArticleLikeDto>();
            //DTO映射到源数据
            CreateMap<ArticleLikeAddDto, ArticleLike>();
            CreateMap<ArticleLikeEditDto, ArticleLike>();
            #endregion
        }

        /// <summary>
        /// 获得扩展字段键值对
        /// </summary>
        /// <param name="articleFields">扩展字段列表</param>
        public static Dictionary<string, string?> GetFields(ICollection<ArticleFieldValue> articleFields)
        {
            Dictionary<string, string?> dic = new();
            foreach (var item in articleFields)
            {
                if (item.FieldName != null)
                {
                    dic.Add(item.FieldName, item.FieldValue);
                }
            }
            return dic;
        }
    }
}
